package org.bozo.im.service.protocols.fake;

import org.bozo.im.activity.Constants;
import org.bozo.im.common.types.Presence;
import org.bozo.im.common.types.Protocol;
import org.bozo.im.common.types.RawContact;
import org.bozo.im.service.IMService;
import org.bozo.im.service.IMSession;
import org.bozo.im.service.IMSessionConfiguration;
import org.bozo.im.service.core.roster.IMRoster;

import android.util.Log;

public class FakeSession extends IMSession {

	private FakeSessionConfiguration mConfiguration;
	
	public FakeSession(IMSessionConfiguration pConfiguration) {
		super(pConfiguration);
		
		if (pConfiguration instanceof FakeSessionConfiguration)
		mConfiguration = (FakeSessionConfiguration)pConfiguration;
	}

	@Override
	public void startSession() throws Exception {
		
		// ---------------------------------------------------------------------------------------
		Log.d(Constants.ACTIVITY_TAG, "org.bozo.im.service.fake.FakeSession.startSession ---------- Creating contact");
		// ---------------------------------------------------------------------------------------
		
		IMRoster lRoster = IMService.getInstance().getRoster();
		
		lRoster.addContact(Protocol.FAKE, "contact1@" + mConfiguration.getFakeSuffix() + ".com", "Benoit Auvinet");
		lRoster.addContact(Protocol.FAKE, "contact2@" + mConfiguration.getFakeSuffix() + ".com", "User1 " + mConfiguration.getFakeSuffix());
		lRoster.addContact(Protocol.FAKE, "contact3@" + mConfiguration.getFakeSuffix() + ".com", "User2 " + mConfiguration.getFakeSuffix());
		lRoster.addContact(Protocol.FAKE, "contact4@" + mConfiguration.getFakeSuffix() + ".com", "User3 " + mConfiguration.getFakeSuffix());
		
		lRoster.pushContacts();
		
		// ---------------------------------------------------------------------------------------
		Log.d(Constants.ACTIVITY_TAG, "org.bozo.im.service.fake.FakeSession.startSession ---------- Retrieving contact");
		// ---------------------------------------------------------------------------------------
		
		RawContact lContactMerged = lRoster.getContact(Protocol.FAKE, "contact1@" + mConfiguration.getFakeSuffix() + ".com");
		RawContact lContact1 = lRoster.getContact(Protocol.FAKE, "contact2@" + mConfiguration.getFakeSuffix() + ".com");
		RawContact lContact2 = lRoster.getContact(Protocol.FAKE, "contact3@" + mConfiguration.getFakeSuffix() + ".com");
		RawContact lContact3 = lRoster.getContact(Protocol.FAKE, "contact4@" + mConfiguration.getFakeSuffix() + ".com");

		// ---------------------------------------------------------------------------------------
		Log.d(Constants.ACTIVITY_TAG, "org.bozo.im.service.fake.FakeSession.startSession ---------- Updating contact");
		// ---------------------------------------------------------------------------------------
		
		lContactMerged.setPresence(Presence.ONLINE);
		lContact1.setPresence(Presence.AWAY);
		lContact2.setPresence(Presence.BUSY);
		lContact3.setPresence(Presence.OFFLINE);
		
		lRoster.itemModified(lContactMerged, RawContact.Field.PRESENCE);
		lRoster.itemModified(lContact1, RawContact.Field.PRESENCE);
		lRoster.itemModified(lContact2, RawContact.Field.PRESENCE);
		lRoster.itemModified(lContact3, RawContact.Field.PRESENCE);

		// ---------------------------------------------------------------------------------------
		Log.d(Constants.ACTIVITY_TAG, "org.bozo.im.service.fake.FakeSession.startSession ---------- Done");
		// ---------------------------------------------------------------------------------------
		
		IMService.getInstance().updateRoster();
	}

	@Override
	public void stopSession() throws Exception {}

	@Override
	public IMSessionConfiguration getConfiguration() {
		return mConfiguration;
	}

}
